package com.mainworks.commons.utils;


import java.io.IOException;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpUtils {

    public static JSONObject sendHttpQequestNoToken(String httpuri, JSONObject data) {
        JSONObject responseData=null;
        if(data !=null&&!"".equals(data.toString())) {
            OkHttpClient client = new OkHttpClient();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, data.toString());
            Request request = new Request.Builder()
                    .url(httpuri)
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("cache-control", "no-cache")
                    .build();

            try {
                Response response = client.newCall(request).execute();
                responseData=JSONObject.parseObject(response.body().string());
                response.close();
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
        return responseData;
    }

    public static JSONObject sendHttpQequest(String httpuri, String data) {
        JSONObject responseData=null;
        if(data !=null&&!"".equals(data)) {
            OkHttpClient client = new OkHttpClient();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, data);
            Request request = new Request.Builder()
                    .url(httpuri)
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("cache-control", "no-cache")
                    .build();

            try {
                Response response = client.newCall(request).execute();
                responseData=JSONObject.parseObject(response.body().string());
                response.close();
            } catch (IOException e) {
                e.printStackTrace();

            }
        }
        return responseData;
    }
}
